﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


using CRM.Entity;
using CRM.BLL;
using DP.Web.UI;
using DP.Common;

namespace CRM.Web.WorkOrder
{
    public partial class StatusModify : Common.PageBase
    {

        T_WORKORDERSTATUS_BLL bll = new T_WORKORDERSTATUS_BLL();
        private T_WORKORDERTYPE_BLL workOrderTypeBll = new T_WORKORDERTYPE_BLL();
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            PageHelper.SetPrePageUrl(this.Page);
            PageHelper.SetGoBackUrl(this.Page, btnCancel, StaticConstant.NotBackUrl);

        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ddlORDERTYPEIDBinding();
                ddlSTATUSBinding();
                ddlTYPEBinding();


                pageDataInit();
            }
        }

       

        #region 页面初始化数据绑定


        /// <summary>
        ///	工单类型编号 下拉列表数据绑定
        /// </summary>
        private void ddlORDERTYPEIDBinding()
        {
            ddlORDERTYPEID.DataSource = workOrderTypeBll.GetCache_T_WORKORDERTYPE_List(this.Page);
            ddlORDERTYPEID.DataTextField = "OrderTypeName";
            ddlORDERTYPEID.DataValueField = "OrderTypeID";
            ddlORDERTYPEID.DataBind();
        }

        /// <summary>
        /// DDLs the TYPE binding.
        /// </summary>
        private void ddlTYPEBinding()
        {
            EnumHelper.FillListControl(ddlTYPE, typeof(StatusType));
        }

        /// <summary>
        /// DDLs the STATUS binding.
        /// </summary>
        private void ddlSTATUSBinding()
        {
            EnumHelper.FillListControl(ddlSTATUS, typeof(Status));
        }









        #endregion

        private void pageDataInit()
        {
            string _orderstatusid = String.IsNullOrEmpty(Request.QueryString["ORDERSTATUSID"]) ? "" : Request.QueryString["ORDERSTATUSID"].Trim();

            if (!String.IsNullOrEmpty(_orderstatusid))
            {
                T_WORKORDERSTATUS_Entity entity = bll.GetEntity("ORDERSTATUSID", _orderstatusid);

                PageHelper.SetControlValue<T_WORKORDERSTATUS_Entity>(this.Page, entity);
                ddlORDERTYPEID.Enabled = false;
            }


        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            string _orderstatusid = String.IsNullOrEmpty(Request.QueryString["ORDERSTATUSID"]) ? "" : Request.QueryString["ORDERSTATUSID"].Trim();


            T_WORKORDERSTATUS_Entity entity = bll.GetEntity("ORDERSTATUSID", _orderstatusid);

            if (entity != null)
            {
                PageHelper.GetControlValue<T_WORKORDERSTATUS_Entity>(this.Page, ref entity);
                entity.ORDERTYPENAME = ddlORDERTYPEID.SelectedItem.Text;
                entity.LASTMODIFYPEOPLE = _userLoginInfo.UserEntity.USERNAME;
                entity.LASTMODIFYPEOPLEID = _userLoginInfo.UserEntity.USERID;
                entity.LASTMODIFYTIME = DateTime.Now;

                try
                {
                    long rel = bll.Update(entity);
                    if (rel > 0)
                    {
                        PageHelper.ShowMessage(this.Page, "修改成功！",
                                               UserLoginInfo.FilterBackUrl(PageHelper.GetPrePageUrl(this), "statuslist.aspx"));
                    }
                    else
                    {
                        PageHelper.ShowMessage(this.Page, "修改失败！");
                    }
                }
                catch (Exception ex)
                {
                    
                        PageHelper.ShowMessage(this.Page, "修改失败！" + ex.Message);
                }
            }
            else
            {
                entity = new T_WORKORDERSTATUS_Entity();

                PageHelper.GetControlValue<T_WORKORDERSTATUS_Entity>(this.Page, ref entity);
                entity.ORDERTYPENAME = ddlORDERTYPEID.SelectedItem.Text;
                entity.CREATEPEOPLE = _userLoginInfo.UserEntity.USERNAME;
                entity.CREATEPEOPLEID = _userLoginInfo.UserEntity.USERID;
                entity.CREATETIME = DateTime.Now;
                entity.LASTMODIFYPEOPLE = _userLoginInfo.UserEntity.USERNAME;
                entity.LASTMODIFYPEOPLEID = _userLoginInfo.UserEntity.USERID;
                entity.LASTMODIFYTIME = DateTime.Now;

                try
                {
                    long rel = bll.Insert(entity);
                    if (rel > 0)
                    {
                        PageHelper.ShowMessage(this.Page, "添加成功！", "statuslist.aspx");
                    }
                    else
                    {
                        PageHelper.ShowMessage(this.Page, "添加失改！");
                    }

                }
                catch (Exception ex)
                {
                    
                        PageHelper.ShowMessage(this.Page, "添加失改！" + ex.Message);
                }
            }
        }
    }
}
